package com.ysd.service.impl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.lis.entity.SysParam;
import com.ysd.lis.entity.SysParamField;
import com.ysd.lis.entity.SysParamValue;
import com.ysd.lis.mapper.sys.SysParamValueMapper;
import com.ysd.lis.request.SysParamFieldValueDto;
import com.ysd.service.SysOptionsService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysOptionsServiceImpl implements SysOptionsService {
    @Autowired
    SysParamValueMapper sysParamValueMapper;

    @Override
    public Map<String, String> getSystemOptions() {
        Map<String, String> options = new HashMap<>();
        options.put("app.name", "LisApplication");
        options.put("app.version", "1.0.0");
        options.put("maintenance.mode", "false");
        return options;
    }

    @Override
    public Map<String, Map<String, String>> findSysParamFieldAndValueList() {
        MPJLambdaWrapper<SysParamValue> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(SysParamField.class, "sf");
        queryWrapper.selectAs(SysParamValue::getId, SysParamFieldValueDto::getValueId);
        queryWrapper.selectAs(SysParamValue::getClassify, SysParamFieldValueDto::getClassify);
        queryWrapper.selectAs(SysParamValue::getClassifyId, SysParamFieldValueDto::getClassifyId);
        queryWrapper.selectAs(SysParamValue::getClassifyCode, SysParamFieldValueDto::getClassifyCode);
        queryWrapper.selectAs(SysParamValue::getValue, SysParamFieldValueDto::getValue);
        queryWrapper.selectAs(SysParamValue::getDataType, SysParamFieldValueDto::getDataType);
        queryWrapper.selectAs(SysParamValue::getIsLocal, SysParamFieldValueDto::getIsLocal);
        queryWrapper.selectAs(SysParamValue::getLocalComputer, SysParamFieldValueDto::getLocalComputer);
        queryWrapper.selectAs(SysParam::getCode, SysParamFieldValueDto::getSysParamCode);
        queryWrapper.selectAs(SysParam::getModuleId, SysParamFieldValueDto::getSysParamModuleId);
        queryWrapper.leftJoin(SysParamField.class, "sf", SysParamField::getId, SysParamValue::getFieldId);
        queryWrapper.leftJoin(SysParamField.class, "sg", SysParamField::getId, SysParamField::getPid);
        queryWrapper.leftJoin(SysParam.class, "sp", SysParam::getId, SysParamField::getPid);
        queryWrapper.eq(SysParamValue::getDelFlag, 0);
        queryWrapper.eq("sf.DEL_FLAG", 0);
        queryWrapper.eq("sg.DEL_FLAG", 0);
        queryWrapper.eq("sp.DEL_FLAG", 0);
        //queryWrapper.eq("sf.grade", 1);
        //queryWrapper.eq("sg.grade", 0);
        List<SysParamFieldValueDto> valueList = sysParamValueMapper.selectJoinList(SysParamFieldValueDto.class, queryWrapper);

        Map<String, List<SysParamFieldValueDto>> groupList = valueList.stream().collect(Collectors.groupingBy(SysParamFieldValueDto::getOrgId));
        Map<String, Map<String, String>> optionsMap = new HashMap<>();

        for (Map.Entry<String, List<SysParamFieldValueDto>> mList : groupList.entrySet()) {
            Map<String, String> options = new HashMap<>();

            for (SysParamFieldValueDto m : mList.getValue()) {
                String key = "";
                if (ToolsUtils.isNotEmpty(m.getSysParamCode())) {
                    key += m.getSysParamCode();
                }
                if (ToolsUtils.isNotEmpty(m.getField())) {
                    key += "." + m.getField();
                }
                if (ToolsUtils.isNotEmpty(m.getClassifyCode())) {
                    key += "." + m.getClassifyCode();
                }
                if (ToolsUtils.isEmpty(key)) {
                    continue;
                }
                options.put(key, m.getValue());
            }
            optionsMap.put(mList.getKey(), options);
        }


        return optionsMap;
    }
}
